package top.healthylife.controller;/*
 *@Author GaoZeXi
 *@Created time 2020/4/20 7:40
 *@Description:
 * Step by Step  and Stand on,  You Are The Best Investment!!!
 */

import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.healthylife.constant.MessageConstant;
import top.healthylife.entity.Result;
import top.healthylife.service.BusinessService;
import top.healthylife.service.MemberService;
import top.healthylife.service.OrderService;
import top.healthylife.service.SetmealService;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report")
public class reportController {
    @Reference
    private MemberService memberService;
    @Reference
    private SetmealService setmealService;
    @Reference
    private BusinessService businessService;
    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        int year =2019;
        Map map= memberService.findMemberCountByMonth(year);
        String monthsStr = (String) map.get("months");
        String[] months = monthsStr.split(",");
        String memberCountStr = (String) map.get("memberCount");
        String[] memberCount = memberCountStr.split(",");
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("months",months);
        dataMap.put("memberCount",memberCount);
        if(dataMap!=null){
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,dataMap);
        }else{
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){

        try {
            List<Map<String,Object>> list = setmealService.findSetmealInOrder();
            Map<String,Object> dataMap =new HashMap(16);
            dataMap.put("setmealCount",list);
            List<String> setmealNames = new ArrayList<>();
            for (Map<String, Object> map : list) {
                String name = (String) map.get("name");
                setmealNames.add(name);
            }
            dataMap.put("setmealNames",setmealNames);
            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,dataMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }

    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        try {
            Map reportData = businessService.getMemberAndOrderInfo();
            List<Map<String, Object>> hotSetmeals = setmealService.getHotSetmeals();
            Map<String,Object> dataMap =new HashMap<>(16);
            dataMap.put("reportData",reportData);
            dataMap.put("hotSetmeal",hotSetmeals);

            return new Result(true, MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,dataMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }
    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request,HttpServletResponse response){
        try {
            Map reportData = businessService.getMemberAndOrderInfo();
            String reportDate = (String) reportData.get("reportDate");
            BigDecimal todayNewMember = (BigDecimal) reportData.get("todayNewMember");
            BigDecimal totalMember = (BigDecimal) reportData.get("totalMember");
            BigDecimal thisWeekNewMember = (BigDecimal) reportData.get("thisWeekNewMember");
            BigDecimal thisMonthNewMember = (BigDecimal) reportData.get("thisMonthNewMember");
            BigDecimal todayOrderNumber = (BigDecimal) reportData.get("todayOrderNumber");
            BigDecimal thisWeekOrderNumber = (BigDecimal) reportData.get("thisWeekOrderNumber");
            BigDecimal thisMonthOrderNumber = (BigDecimal) reportData.get("thisMonthOrderNumber");
            BigDecimal todayVisitsNumber = (BigDecimal) reportData.get("todayVisitsNumber");
            BigDecimal thisWeekVisitsNumber = (BigDecimal) reportData.get("thisWeekVisitsNumber");
            BigDecimal thisMonthVisitsNumber = (BigDecimal) reportData.get("thisMonthVisitsNumber");
            List<Map<String, Object>> hotSetmeals = setmealService.getHotSetmeals();

            //获取Excel模板文件绝对路径,File.separator 获取当前系统对应的分隔符,windows 是\,linux 是/
            String templateRealPath =
                                request.getSession().getServletContext().getRealPath("template")+
                                        File.separator+ "report_template.xlsx";
            //读取模板文件,基于模板文件 创建一个Excel表格
            XSSFWorkbook excel =
                    new XSSFWorkbook(new FileInputStream(new File(templateRealPath)));
            //获取第一页工作表
            XSSFSheet sheetOne = excel.getSheetAt(0);
            //第三行第六个,日期
            sheetOne.getRow(2).getCell(5).setCellValue(reportDate);
            //第5行,第6个, 新增会员
            sheetOne.getRow(4).getCell(5).setCellValue(todayNewMember.intValue());
            //第5行,第8个 总会员数
            sheetOne.getRow(4).getCell(7).setCellValue(totalMember.intValue());
            //第6行,第6个 本周新增会员数
            sheetOne.getRow(5).getCell(5).setCellValue(thisWeekNewMember.intValue());
            //第6行,第8个 本月新增会员数
            sheetOne.getRow(5).getCell(7).setCellValue(thisMonthNewMember.intValue());

            //第8行,第6个
            sheetOne.getRow(7).getCell(7).setCellValue(todayOrderNumber.intValue());
            //第8行,第8个
            sheetOne.getRow(7).getCell(7).setCellValue(todayVisitsNumber.intValue());

            //第9行,第6个
            sheetOne.getRow(8).getCell(5).setCellValue(thisWeekOrderNumber.intValue());
            //第9行,第8个
            sheetOne.getRow(8).getCell(7).setCellValue(thisWeekVisitsNumber.intValue());

            //第10行,第6个
            sheetOne.getRow(9).getCell(5).setCellValue(thisMonthOrderNumber.intValue());
            //第10行,第8个
            sheetOne.getRow(9).getCell(7).setCellValue(thisMonthVisitsNumber.intValue());
            //本周热门套餐
            int rowNum=12;
            for (Map<String, Object> hotSetmeal : hotSetmeals) {
                String name = (String) hotSetmeal.get("name");
                Long setmealCount = (Long) hotSetmeal.get("setmealCount");
                String countPercent = (String) hotSetmeal.get("countPercent");
                sheetOne.getRow(rowNum).getCell(4).setCellValue(name);
                sheetOne.getRow(rowNum).getCell(5).setCellValue(setmealCount);
                sheetOne.getRow(rowNum).getCell(6).setCellValue(countPercent);
                sheetOne.getRow(rowNum).getCell(7).setCellValue("无");
                rowNum++;

            }
            //使用输出流进行表格下载,基于浏览器作为客户端下载
            ServletOutputStream outputStream = response.getOutputStream();
            //代表的是Excel文件类型
            response.setContentType("appliation/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename=report_Business.xlsx");
            excel.write(outputStream);
            //关闭输出流
            outputStream.close();
            return new Result(true, "运营数据统计信息导出成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }

    }

}
